package com.emep.wechat.wechatdemo.controller;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * 消息推送
 * @author wangcc
 */
@RestController
@RequestMapping("msg")
public class WechatTemplateMsgController {

    @Autowired
    private WxMpService wxService;

    @GetMapping("push")
    public String push(@RequestParam String companyName,
                       @RequestParam String outletName,
                       @RequestParam String dataType) throws WxErrorException {

            /**
             *
             *模版样本
             *
             *企业名称：{{first.DATA}} 排口名称：{{first1.DATA}} 超标类型：{{first2.DATA}}
             *
             */

            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    //模版id
                    .toUser("oePJbw_OhCcTIoiMNCVXbTdF8mPs")
                    //接受人opendId
                    .templateId("pkX7E1lq1UOvM5LhHSGuWm5VCf0fL17nYn4jge51WU4")
                    .build();
            templateMessage.getData().add(new WxMpTemplateData("first", companyName, "#FF00FF"));
            templateMessage.getData().add(new WxMpTemplateData("first1", outletName, "#FF00FF"));
            templateMessage.getData().add(new WxMpTemplateData("first2", dataType, "#FF00FF"));
            String msgId = this.wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            System.out.println(msgId);
        return msgId;

    }
}
